import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getUserIfo } from '@/api/user'

export const useUserStore = defineStore(
  'userStore',
  () => {
    const token = ref()
    // 设置token
    const setToken = (newToken) => {
      token.value = newToken
    }
    // 移除token
    const removeToken = () => {
      token.value = ''
    }

    const usrNamePassw = ref({}) //用户的密码和账户名字

    const userSave_NP = (obj) => {
      usrNamePassw.value = obj
    }
    const userData = ref({}) // 文章分类内容存储

    const loading = ref(true)
    const getUser = async () => {
      const res = await getUserIfo() // 请求获取数据
      loading.value = false
      userData.value = res.data.data
    }

    const setUser = (obj) => {
      userData.value = obj
    }
    return {
      token,
      setToken,
      userSave_NP,
      usrNamePassw,
      removeToken,
      userData,
      getUser,
      setUser
    }
  },
  {
    persist: {
      key: 'mytoken',
      paths: ['token', 'usrNamePassw']
    }
  }
)
